package model

import (
	"encoding/json"
	"gorm.io/gorm"
	"iot-base/auth/config"
	"iot-base/auth/db"
	"iot-base/auth/utils"
	"time"
)

type InitTable struct {
	db *gorm.DB
}

func NewInitTable() *InitTable {
	return &InitTable{
		db: db.Db,
	}
}

// CreateTable 初始花表
func (t *InitTable) CreateTable() {
	migrator := t.db.Migrator()
	if !migrator.HasTable(&User{}) {
		migrator.CreateTable(&User{}, &AuthRole{}, &AuthGroup{}, &AuthRule{}, &UserRole{}, &UserGroup{}, &AuthGroupStations{}, &Company{},
			&TaskLog{}, &Tasks{}, &AuthRule{}, &Presets{}, &OperationLogs{})
		InitAuthRules()
		// 初始化用户
		var user User
		result := t.db.First(&user)
		if result.RowsAffected == 0 {
			tx := t.db.Begin()
			//初始化管理组
			var authGroup AuthGroup
			authGroup.ID = 1
			authGroup.GroupName = "系统"
			user.CreatedAt = time.Now()
			user.UpdatedAt = time.Now()
			if err := tx.Create(&authGroup).Error; err != nil {
				tx.Rollback()
				return
			}
			// 初始化化管理员 组和角色
			var authRole AuthRole
			authRole.ID = 1
			authRole.RoleName = "管理员"
			authRole.Rules = "*"
			user.CreatedAt = time.Now()
			user.UpdatedAt = time.Now()
			if err := tx.Create(&authRole).Error; err != nil {
				tx.Rollback()
				return
			}
			var company Company
			company.Name = "管理"
			company.GroupId = authGroup.ID
			if err := tx.Create(&company).Error; err != nil {
				tx.Rollback()
				panic(err)
				return
			}

			user.ID = utils.GetUid()
			user.UserName = config.Conf.DefaultUser.UserName
			user.UserType = config.Conf.DefaultUser.UserType
			user.Salt = utils.GetRandomString(6)
			user.PassWord = utils.GetMd5Pwd(utils.GetMd5(config.Conf.DefaultUser.PassWord), user.Salt)
			user.CreatedAt = time.Now()
			user.UpdatedAt = time.Now()
			user.CompanyId = company.ID
			if err := tx.Create(&user).Error; err != nil {
				tx.Rollback()
				panic(err)
				return
			}

			var userGroup UserGroup
			userGroup.Uid = user.ID
			userGroup.RoleId = authRole.ID
			userGroup.GroupId = authGroup.ID
			if err := tx.Create(&userGroup).Error; err != nil {
				tx.Rollback()
				return
			}
			tx.Commit()
		}
	}
}

func (t *InitTable) CreateGate() {
	migrator := t.db.Migrator()
	migrator.AutoMigrate(&GateRule{})
	//if !migrator.HasTable(&DoorRule{}) {
	//	migrator.AutoMigrate(&DoorRule{})
	//}
}

func InitAuthRules() {
	//authRole, err := ioutil.ReadFile("./auth_rlues.json")
	//if err != nil {
	//	return
	//}
	//if len(authRole) == 0 {
	//	return
	//}

	authString := `[{"id":942006710400,"type":1,"pid":941957070976,"title":"用户管理","auth_rule":"","router_name":"","path":"","method":"","icon":"","weigh":0,"auth_code":"","remark":"","extend":"","router_path":""},{"id":941620842624,"type":1,"pid":0,"title":"站点控板","auth_rule":"","router_name":"monitor","path":"/monitor/monitor.vue","method":"","icon":"monitor","weigh":0,"auth_code":"","remark":"","extend":"","router_path":""},{"id":963431002240,"type":1,"pid":942006710400,"title":"角色管理","auth_rule":"/auth/role","router_name":"role","path":"/user/role.vue","method":"GET","icon":"","weigh":0,"auth_code":"","remark":"","extend":"","router_path":""},{"id":943659176064,"type":1,"pid":942006710400,"title":"分组管理","auth_rule":"","router_name":"group","path":"/user/group.vue","method":"","icon":"","weigh":0,"auth_code":"","remark":"","extend":"","router_path":"/group"},{"id":943564857472,"type":1,"pid":942006710400,"title":"账号管理","auth_rule":"","router_name":"user","path":"/user/user.vue","method":"","icon":"","weigh":0,"auth_code":"","remark":"","extend":"","router_path":"/user"},{"id":965573464192,"type":1,"pid":0,"title":"数据查询","auth_rule":"","router_name":"data","path":"/data/data.vue","method":"","icon":"data","weigh":5,"auth_code":"","remark":"","extend":"","router_path":"/data"},{"id":13318551232640,"type":1,"pid":942006710400,"title":"操作日志","auth_rule":"/operationLogs","router_name":"logs","path":"/log/log","method":"GET","icon":"","weigh":1,"auth_code":"","remark":"","extend":"","router_path":"/logs"},{"id":4144175521920,"type":1,"pid":0,"title":"视频监控","auth_rule":"","router_name":"video","path":"/video/video.vue","method":"","icon":"video","weigh":1,"auth_code":"","remark":"","extend":"","router_path":"/video"},{"id":941957070976,"type":1,"pid":0,"title":"权限管理","auth_rule":"","router_name":"","path":"","method":"","icon":"sys","weigh":6,"auth_code":"sys","remark":"","extend":"","router_path":""},{"id":3346854264960,"type":1,"pid":0,"title":"站点管理","auth_rule":"","router_name":"","path":"","method":"","icon":"station","weigh":5,"auth_code":"","remark":"","extend":"","router_path":""},{"id":28469998960768,"type":1,"pid":3346854264960,"title":"站点配置","auth_rule":"","router_name":"station","path":"/station/station.vue","method":"","icon":"","weigh":0,"auth_code":"","remark":"站点、设备管理","extend":"","router_path":"/station"},{"id":28474840121472,"type":1,"pid":3346854264960,"title":"物模型管理","auth_rule":"","router_name":"tmodel","path":"/station/tmodel","method":"","icon":"","weigh":1,"auth_code":"","remark":"","extend":"","router_path":"/tmodel"},{"id":28475196956800,"type":1,"pid":3346854264960,"title":"数据调试","auth_rule":"","router_name":"tester","path":"/station/tester","method":"","icon":"","weigh":2,"auth_code":"","remark":"","extend":"","router_path":"/tester"},{"id":3346566176896,"type":1,"pid":0,"title":"系统设置","auth_rule":"","router_name":"set","path":"/set/set","method":"","icon":"set","weigh":4,"auth_code":"","remark":"","extend":"","router_path":"/set"},{"id":80218580770944,"type":1,"pid":0,"title":"动环监控","auth_rule":"","router_name":"pwevn","path":"/pwevn/pwevn","method":"","icon":"evn","weigh":1,"auth_code":"","remark":"","extend":"","router_path":"/pwevn"},{"id":80229029134464,"type":1,"pid":0,"title":"质控管理","auth_rule":"","router_name":"","path":"","method":"","icon":"qc","weigh":2,"auth_code":"","remark":"","extend":"","router_path":""},{"id":3345698537600,"type":1,"pid":80229029134464,"title":"仪器校准","auth_rule":"","router_name":"equipment","path":"/equipment/equipment.vue","method":"","icon":"tml","weigh":0,"auth_code":"","remark":"","extend":"","router_path":"/equipment"},{"id":9084764020864,"type":1,"pid":80229029134464,"title":"质控计划","auth_rule":"","router_name":"qc","path":"/qc/qc","method":"","icon":"qc","weigh":1,"auth_code":"","remark":"","extend":"","router_path":"/qc"}]`
	authRole := []byte(authString)
	authRules := make([]AuthRule, 1)
	json.Unmarshal(authRole, &authRules)
	NewAuthRuleModel(db.Db).InitData(authRules)

}
